package JUnitTests;

import static org.junit.Assert.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import DataStorageClasses.*;
import Database.DatabaseConnector;

public class JobConnectorTests 
{
	private static DatabaseConnector dbConnector;
	private static long jobID;
	private static long clientID;
	private static String cFirstName;
	private static String cLastName;
	private static String businessName;
	private static String busPassword;
	
	@BeforeClass static public void intitialize()
	{
		dbConnector = new DatabaseConnector();
		dbConnector.connect();
		
		businessName = "TesterBusiness";
		busPassword = "Password";
		cFirstName = "Tester";
		cLastName = "Name";
		
		dbConnector.addBusiness(businessName, busPassword, null, null, null, null, null, null, null, null, null);
		Client client = dbConnector.addClient(cFirstName, cLastName, null, null, null, null, null, null, null, null, businessName);
		clientID = client.getClientId();
		
		Job job = dbConnector.addNewJob(clientID, -1, -1, -1, null, null, null, null, null, null, null, businessName);
		jobID = job.getJobID();
	}
	
	@AfterClass static public void destroy()
	{
		boolean removed = dbConnector.removeJob(jobID);
		assertTrue(removed);
		removed = dbConnector.removeClient(clientID);
		assertTrue(removed);
		dbConnector.removeBusiness(businessName, busPassword);
		assertTrue(removed);
		
		dbConnector.disconnect();
	}

	@Test public void testAddJob()
	{
		Job job = dbConnector.addNewJob(-1, -1, -1, -1, null, null, null, null, null, null, null, businessName);
		assertTrue(job == null);
		
		job = dbConnector.addNewJob(jobID, -1, -1, -1, null, null, null, null, null, null, null, "wrong business");
		assertTrue(job == null);
	}
	
	@Test public void testUpdateJob()
	{
		double newEstP = 12;
		double newActP = 423;
		double newAmtP = 342;
		Date newExpSDate = new Date(0);
		Date newActSDate = new Date(1);
		Date newExpFDate = new Date(2);
		Date newActFDate = new Date(3);
		String newStreet = "new street";
		String newCity = "new city";
		String newProvince = "new province";
		
		boolean updated = dbConnector.updateJob(jobID, newEstP, newActP, newAmtP, newExpSDate, newActSDate, newExpFDate, 
				newActFDate, newStreet, newCity, newProvince);
		
		assertTrue(updated);
		
		Job job = dbConnector.getJob(jobID);
		
		assertTrue(job != null);
		
		assertTrue(newEstP == job.getEstimatedPrice());
		assertTrue(newActP == job.getActualPrice());
		assertTrue(newAmtP == job.getAmountPaid());
		assertArrayEquals(newExpSDate.toString().toCharArray(), job.getExpectedStartDate().toString().toCharArray());
		assertArrayEquals(newActSDate.toString().toCharArray(), job.getActualStartDate().toString().toCharArray());
		assertArrayEquals(newExpFDate.toString().toCharArray(), job.getExpectedFinishDate().toString().toCharArray());
		assertArrayEquals(newActFDate.toString().toCharArray(), job.getActualFinishDate().toString().toCharArray());
		assertArrayEquals(newStreet.toCharArray(), job.getStreetAdress().toCharArray());
		assertArrayEquals(newCity.toCharArray(), job.getCity().toCharArray());
		assertArrayEquals(newProvince.toCharArray(), job.getProvince().toCharArray());
		
		updated = dbConnector.updateJob(-1, newEstP, newActP, newAmtP, newExpSDate, newActSDate, newExpFDate, 
				newActFDate, newStreet, newCity, newProvince);
		assertFalse(updated);
	}
	
	@Test public void testGetClientJobs()
	{
		long idclient;
		long idjob;
		
		Client client = dbConnector.addClient(cFirstName, cLastName, null, null, null, null, null, null, null, null, businessName);
		idclient = client.getClientId();
		
		Job job = dbConnector.addNewJob(idclient, -1, -1, -1, null, null, null, null, null, null, null, businessName);
		idjob = job.getJobID();
		
		ArrayList<Job> jobs = dbConnector.getClientsJobs(idclient, businessName);
		assertTrue(jobs.size() == 1);
		assertTrue(jobs.get(0).getJobID() == idjob);
		
		dbConnector.removeJob(idjob);
		dbConnector.removeClient(idclient);
	}
	
	@Test public void testGetEmployeeJobs()
	{
		String employee1 = "emp1";
		long emp1ID;
		String employee2 = "emp2";
		long emp2ID;
		long job1ID;
		long job2ID;
		
		emp1ID = dbConnector.addEmployee(businessName, employee1, null, null, null, -1, -1, null, null, null, null, null, null).getEmployeeID();
		emp2ID = dbConnector.addEmployee(businessName, employee2, null, null, null, -1, -1, null, null, null, null, null, null).getEmployeeID();
		job1ID = dbConnector.addNewJob(clientID, -1, -1, -1, null, null, null, null, null, null, null, businessName).getJobID();
		job2ID = dbConnector.addNewJob(clientID, -1, -1, -1, null, null, null, null, null, null, null, businessName).getJobID();
		
		dbConnector.addTimeSheet(jobID, emp1ID, new Date(0), 4, false);
		dbConnector.addTimeSheet(job1ID, emp1ID, new Date(1), 4, false);
		dbConnector.addTimeSheet(job2ID, emp2ID, new Date(2), 5, false);
		
		ArrayList<Job> jobs = dbConnector.getEmployeesJobs(emp1ID, businessName);
		assertTrue(jobs.size() == 2);
		
		jobs = dbConnector.getEmployeesJobs(emp2ID, businessName);
		assertTrue(jobs.size() == 1);
		
		dbConnector.removeTimeSheet(emp1ID, jobID, new Date(0));
		dbConnector.removeTimeSheet(emp1ID, job1ID, new Date(1));
		dbConnector.removeTimeSheet(emp2ID, job2ID, new Date(2));
		
		dbConnector.removeJob(job1ID);
		dbConnector.removeJob(job2ID);
		dbConnector.removeEmployee(emp1ID);
		dbConnector.removeEmployee(emp2ID);
	}
	
	@Test public void testGetJobsBetweenDates()
	{
		Calendar calender = Calendar.getInstance();
		
		long job1;
		long job2;
		long job3;
		long job4;
		long job5;
		
		calender.set(2009, 3, 4);
		Date date1S = new Date(calender.getTimeInMillis());
		calender.set(2009, 4, 5);
		Date date1F = new Date(calender.getTimeInMillis());
		
		calender.set(2010,11,21);
		Date date2S = new Date(calender.getTimeInMillis());
		calender.set(2011,1, 22);
		Date date2F = new Date(calender.getTimeInMillis());
		
		calender.set(2000, 11, 22);
		Date date3S = new Date(calender.getTimeInMillis());
		calender.set(2001, 3, 5);
		Date date3F = new Date(calender.getTimeInMillis());
		
		calender.set(2000, 11, 21);
		Date date4S = new Date(calender.getTimeInMillis());
		calender.set(2000, 11, 25);
		Date date4F = new Date(calender.getTimeInMillis());
		
		job1 = dbConnector.addNewJob(clientID, -1, -1, -1, date1S, date1S, date1F, date1F, null, null, null, businessName).getJobID();
		job2 = dbConnector.addNewJob(clientID, -1, -1, -1, date2S, date2S, date2F, date2F, null, null, null, businessName).getJobID();
		job3 = dbConnector.addNewJob(clientID, -1, -1, -1, date3S, date3S, date3F, date3F, null, null, null, businessName).getJobID();
		job4 = dbConnector.addNewJob(clientID, -1, -1, -1, date4S, date4S, date4F, date4F, null, null, null, businessName).getJobID();
		
		calender.set(2009, 0, 1);
		Date low = new Date(calender.getTimeInMillis());
		
		calender.set(2010, 0, 1);
		Date high = new Date(calender.getTimeInMillis());
		
		ArrayList<Job> jobs = dbConnector.getJobsBetweenDates(businessName, low, high, null, null, null, null, null, null, 1);
		assertTrue(jobs.size() == 1);
		
		calender.set(2010, 11, 25);
		high = new Date(calender.getTimeInMillis());
		
		jobs = dbConnector.getJobsBetweenDates(businessName, low, high, null, null, null, null, null, null, 1);
		assertTrue(jobs.size() == 2);
		
		jobs = dbConnector.getJobsBetweenDates(businessName, null, null, null, null, null, null, null, null, 1);
		assertTrue(jobs.size() == 5);
		
		jobs = dbConnector.getJobsBetweenDates(businessName, null, null, null, null, null, null, null, null, 0);
		assertTrue(jobs.size() == 1);
		
		jobs = dbConnector.getJobsBetweenDates(businessName, null, null, null, null, null, null, null, null, -1);
		assertTrue(jobs.size() == 4);
		
		dbConnector.removeJob(job1);
		dbConnector.removeJob(job2);
		dbConnector.removeJob(job3);
		dbConnector.removeJob(job4);
	}
	
	@Test public void testRemoveJob()
	{
		long testJobID = dbConnector.addNewJob(clientID, -1, -1, -1, null, null, null, null, null, null, null, businessName).getJobID();
		long wrongID = -1;
		
		boolean removed = dbConnector.removeJob(wrongID);
		Job job = dbConnector.getJob(jobID);
		assertFalse(removed);
		assertNotNull(job);
		
		removed = dbConnector.removeJob(testJobID);
		job = dbConnector.getJob(testJobID);
		assertTrue(removed);
		assertNull(job);
	}
}
